﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace WinnerCMS.WebFactory.Mvc
{
    public class TemplateViewEngine : VirtualPathProviderViewEngine
    {
        public TemplateViewEngine(string template)
        {
            base.MasterLocationFormats = new[] {
                "~/Templates/" + template + "/{1}/{0}.master",
                "~/Templates/" + template + "/Shared/{0}.master",
                "~/Templates/Shared/{0}.master"
            };

            base.ViewLocationFormats = new[] {
                "~/Templates/" + template + "/{0}",
                "~/Templates/" + template + "/{1}/{0}.aspx",
                "~/Templates/" + template + "/{1}/{0}.ascx",
                "~/Templates/" + template + "/Shared/{0}.aspx",
                "~/Templates/" + template + "/Shared/{0}.ascx",
                "~/Templates/Shared/{1}/{0}.aspx",
                "~/Templates/Shared/{1}/{0}.ascx",
                "~/Templates/Shared/{0}.aspx",
                "~/Templates/Shared/{0}.ascx"
            };
            this.Template = template;
            base.PartialViewLocationFormats = base.ViewLocationFormats;
        }
        private string Template { get; set; }
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            return new WebFormView(controllerContext, partialPath);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            return new WebFormView(controllerContext, viewPath, masterPath);
        }

        protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            return true;
        }

        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName,
                                                  string masterName, bool useCache)
        {
            var viewfile = "~/Templates/" + Template + viewName;

            var ver = new ViewEngineResult(new[] { viewfile });

            return ver;
        }
    }
}